package com.xry.controller;

import com.xry.service.impl.DeepSeekService;
import com.xry.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; // 引入Jackson的ObjectMapper

@RestController
@CrossOrigin//允许跨域
@RequestMapping("/deepseek")
public class DeepSeekController {

    @Autowired
    private DeepSeekService deepSeekService;

    // 注入Jackson的ObjectMapper用于JSON解析
    private final ObjectMapper objectMapper = new ObjectMapper();

    @PostMapping("/chat")
    public Result chat(@RequestBody String prompt) {
        try {
            String result = deepSeekService.sendRequest(prompt);

            // 关键修改：使用Jackson解析JSON，而非Gson
            JsonNode jsonResult = objectMapper.readTree(result);

            return Result.success(jsonResult); // 返回Jackson的JsonNode（可正常序列化）
        } catch (Exception e) {
            return Result.fail(500, e.getMessage());
        }
    }
}